package com.decent.ejfadmin.job;

import com.decent.ejfadmin.common.bean.SupUser;
import com.decent.ejfadmin.common.bean.SupplyBalance;
import com.decent.ejfadmin.common.bean.SupplyConsumption;
import com.decent.ejfadmin.finance.dao.SupplyDailySheetDao;
import com.decent.ejfadmin.finance.service.SupplyFinanceService;
import com.decent.ejfadmin.user.dao.UserDao;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 保存每日期初余额定时器
 *
 * @author HEJUNLEI
 */
@Component
@Configurable
public class DailySupplyMoneySaveJob {
    private static final Logger LOGGER = LoggerFactory.getLogger(DailySupplyMoneySaveJob.class);
    @Autowired
    private SupplyDailySheetDao supplyDailySheetDao;
    @Autowired
    private SupplyFinanceService supplyFinanceService;
    @Autowired
    private UserDao userDao;

    @Scheduled(cron = "0 20 0 * * ?")
    public void doScheduledJob() {

        //期初余额
        SupplyBalance beginBalance = new SupplyBalance();
        //昨日时间戳
        String yesterday = DateFormatUtils.format(DateUtils.addDays(new Date(), -1), "yyyy-MM-dd");
        //今日时间戳
        String today = DateFormatUtils.format(new Date(), "yyyy-MM-dd");
        //获取当前所有用户的余额
        List<SupUser> supplyUsers = userDao.queryAllUserNotParam();
        for (SupUser user : supplyUsers) {
            beginBalance.setUserId(user.getUserId());
            try {
                //查询昨日消费记录
                SupplyConsumption consumption = supplyDailySheetDao.queryUserSupplyConsumption(user.getUserId(), yesterday, "desc");
                if (consumption == null) {
                    //表示没有消费，查询今日消费记录
                    consumption = supplyDailySheetDao.queryUserSupplyConsumption(user.getUserId(), today, "asc");
                    if (consumption == null) {
                        //今日没有消费，当前可提现余额作为期初余额
                        SupplyBalance supplyBalance = supplyFinanceService.querySupplyBalanceVersion(user.getUserId());
                        beginBalance.setBalance(supplyBalance.getBalance());
                        supplyDailySheetDao.saveDailyBalance(beginBalance, today);
                        continue;
                    }
                    //按照今日第一笔计算期初余额
                    //消费明细余额+（消费金额*-1）
                    beginBalance.setBalance(consumption.getBalance().add(consumption.getMoney().multiply(new BigDecimal(-1))));
                    supplyDailySheetDao.saveDailyBalance(beginBalance, today);
                    continue;
                }
                //昨天有消费，按照最后一笔计算期初余额
                beginBalance.setBalance(consumption.getBalance());
                supplyDailySheetDao.saveDailyBalance(beginBalance, today);
            } catch (Exception e) {
                LOGGER.error("{} 保存会员每日余额定时器异常: {}", user, e);
            }
        }
    }
}
